layui.extend({
    baseConfig : "/js/pages/baseConfig"
})
layui.use(['form','layer','element','util','upload','baseConfig'],function(){

    var form = layui.form
    layer = parent.layer === undefined ? layui.layer : top.layer,
        laypage = layui.laypage,
        upload = layui.upload,
        layedit = layui.layedit,
        laydate = layui.laydate,
        baseConfig = layui.baseConfig,
        $ = layui.jquery;

    var imgFilePath = decodeURIComponent( getUrlParamer( "customsClearanceFormImg"));

    form.verify({
        trackingNumber : function(val){
            if( !val || val == ''){
                return "快递单号为必选";
            }
            var checkRule = /^[a-zA-Z0-9]{1,15}$/;
            if( !checkRule.test( val)){
                return "快递单号为1-15位数字加英文";
            }
        }
    });

    //上传发票
    form.on("submit(submitBtn)",function( data){
        //弹出loading
        var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});

        //提交数据
        $.post("/sendGoodsRecord/addOrUpdate",{
            id : data.field.id,
            customsClearanceFormPdf : data.field.customsClearanceFormPdf,
            customsClearanceFormImg : data.field.customsClearanceFormImg,
            trackingNumber : data.field.trackingNumber
        },function( res){
            if( res.code == 200){
                setTimeout(function(){
                    top.layer.close( index);
                    top.layer.msg( "上传出关单成功");
                    layer.closeAll( "iframe");
                    //刷新父页面
                    parent.location.reload();
                },500);
            }else{
                top.layer.close( index);
                top.layer.msg( res.msg);
            }
        });
    });

    //审核发票
    form.on("submit(reviewBtn)",function( data){
        //弹出loading
        var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});

        //提交数据
        $.post("/sendGoodsRecord/addOrUpdate",{
            id : data.field.id,
            reviewStatus : data.field.reviewStatus,
            reviewMemo : data.field.reviewMemo
        },function( res){
            if( res.code == 200){
                setTimeout(function(){
                    top.layer.close( index);
                    top.layer.msg( "审核成功");
                    layer.closeAll( "iframe");
                    //刷新父页面
                    parent.location.reload();
                },500);
            }else{
                top.layer.close( index);
                top.layer.msg( res.msg);
            }
        });
    });

    //弹出层
    $("#pdfImg").on('click', function() {
        var src = $( "#pdfImg").attr( "src");
        if( !src){
            return;
        }
        //客户嫌弹窗打开的图片太小了，这里商议之后直接新窗口打开即可
        window.open( src)
    });

    //上传发票
    upload.render({
        elem: '.uploadCustomsClearanceFormPdf',
        url: '/file/uploadPdf?type=customsClearanceFormPdf',
        method : "post",
        accept: "file",
        exts: "pdf",
        done: function( res){
            if( res.code == 200){
                $("input[name=customsClearanceFormPdf]").val( res.data.pdfFilePath);
                $("input[name=customsClearanceFormImg]").val( res.data.imgFilePaths);
                handlemuliteImgs( res.data.imgFilePaths);
            }
        }
    });

    if( imgFilePath){
        handlemuliteImgs( imgFilePath);
    }

    //处理多张图片如何显示的问题
    function handlemuliteImgs( imgFilePath){
        var imgFilePaths = imgFilePath.split( ";");
        var buttonDivStr = "";
        var imgDivStr = "";
        var i = 0;
        if( imgFilePaths.length == 1){//单张
            var imgFilePath = imgFilePaths[0];
            imgDivStr = "<img class='customs_pdfImg_class' src=\"" + imgFilePath + "\" id=\"pdfImg\"/>";
        }else{//多张
            for( ; i< imgFilePaths.length; i++){
                var imgFilePath = imgFilePaths[i];
                if( imgFilePath && imgFilePath != ""){
                    if( i == 0){
                        imgDivStr += "<img class='customs_pdfImg_class' src=\"" + imgFilePath + "\" id=\"pdfImg" + (i+1) + "\"/>";
                        buttonDivStr += "<a class='pdfBtn_class isChoose' id=\"btn" + (i+1) + "\" for=\"pdfImg" + (i+1) + "\">" + (i+1) + "</a>";
                    }else{
                        imgDivStr += "<img class='customs_pdfImg_class' style='display: none;' src=\"" + imgFilePath + "\" id=\"pdfImg" + (i+1) + "\"/>";
                        buttonDivStr += "<a class='pdfBtn_class' id=\"btn" + (i+1) + "\" for=\"pdfImg" + (i+1) + "\">" + (i+1) + "</a>";
                    }
                }
            }
        }
        $( "#pdfImgDiv").html( imgDivStr);
        $( "#button_div").html( buttonDivStr);
        for( var j = 0; j<= i; j++){
            //给按钮绑定点击事件
            $( "#button_div").on( "click", "#btn" + (j+1),function(){
                var p = $( this).attr( "for");
                $( ".customs_pdfImg_class").css( "display", "none");
                $( "#" + p).css( "display", "block");
                $( ".pdfBtn_class").removeClass( "isChoose");
                $( this).addClass( "isChoose");
                $("#pdfImg_big").attr( "src", $( "#" + p).attr( "src"));
                //弹出层
                $("#" + p).on('click', function() {
                    var src = $( this).attr( "src");
                    if( !src){
                        return;
                    }
                    //客户嫌弹窗打开的图片太小了，这里商议之后直接新窗口打开即可
                    window.open( src);
                });
            });
        }
    }
});
